package com.lzb.play.adapter;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.blankj.utilcode.util.FileUtils;
import com.blankj.utilcode.util.TimeUtils;
import com.blankj.utilcode.util.Utils;
import com.lzb.play.R;
import com.lzb.play.basic.media.IAudioPlayer;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

public class RecordAdapter extends RecyclerView.Adapter<RecordAdapter.RecordViewHolder> {

    private List<String> mAudioPaths = new ArrayList<>();
    private IAudioPlayer mAudioPlayer;
    public IAudioPlayer mCurAudioPlayer;
    private RecordViewHolder mPlayHolder;
    private OnItemClickListener listener;

    public RecordAdapter(IAudioPlayer audioPlayer) {
        mAudioPlayer = audioPlayer;
        mAudioPlayer.setListener(mAudioPlayListener);
    }

    public void addAudioPath(String audioPath) {
        mAudioPaths.add(audioPath);
        notifyItemInserted(mAudioPaths.size() - 1);
    }

    public void addAudioPaths(List<String> audioPaths) {
        int startPosition = mAudioPaths.size();
        mAudioPaths.addAll(audioPaths);
        notifyItemRangeInserted(startPosition, audioPaths.size());
    }

    public void setAudioPaths(List<String> mAudioPaths) {
        this.mAudioPaths = mAudioPaths;
        notifyDataSetChanged();
    }

    public void removeAudioPath(int position) {
        if (position >= 0 && position < mAudioPaths.size()) {
            mAudioPaths.remove(position);
            notifyItemRemoved(position);
            if (position < mAudioPaths.size()) {
                notifyItemRangeChanged(position, mAudioPaths.size() - position);
            }
        }
    }

    public void clear() {
        int size = mAudioPaths.size();
        mAudioPaths.clear();
        notifyItemRangeRemoved(0, size);
    }

    public List<String> getAudioPaths() {
        return new ArrayList<>(mAudioPaths);
    }

    @NonNull
    @Override
    public RecordViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.item_record, parent, false);
        return new RecordViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull RecordViewHolder holder, int position) {
        holder.bindData(mAudioPaths.get(position), position);
    }

    @Override
    public int getItemCount() {
        return mAudioPaths.size();
    }

    private void handlePlayPause(RecordViewHolder holder, int position) {
        if (mPlayHolder == holder) {
            if (mCurAudioPlayer != null && mCurAudioPlayer.isPlaying()) {
                mCurAudioPlayer.pause();
            }
            if (mAudioPlayer.isPlaying()) {
                mAudioPlayer.pause();
            } else {
                mAudioPlayer.resume();
            }
            return;
        }

        if (mPlayHolder != null) {
            mPlayHolder.mIvPlayPause.setImageResource(R.drawable.play2);
        }

        if (mCurAudioPlayer != null && mCurAudioPlayer.isPlaying()) {
            mCurAudioPlayer.pause();
        }

        mPlayHolder = holder;
        mAudioPlayer.play(mAudioPaths.get(position));
    }

    private void handleDel(RecordViewHolder holder, int position) {
        if (mPlayHolder == holder) {
            mAudioPlayer.stop();
            mPlayHolder = null;
        }
        if (listener != null) {
            listener.onItemDelete(position);
        }
    }

    private IAudioPlayer.IListener mAudioPlayListener = new IAudioPlayer.IListener() {
        @Override
        public void onUpdatePlayTime(int curPlayTime, int duration) {
            if (mPlayHolder != null) {
                // 可以在这里更新播放进度
            }
        }

        @Override
        public void onPlayChange(boolean isPlaying) {
            if (mPlayHolder != null) {
                mPlayHolder.mIvPlayPause.setImageResource(isPlaying ? R.drawable.stop2 : R.drawable.play2);
            }
        }
    };

    public class RecordViewHolder extends RecyclerView.ViewHolder {
        public TextView mTvName;
        public TextView mTvTime;
        public ImageView mIvDel;
        public ImageView mIvPlayPause;
        public String mBindPath;
        public int mBindPos;

        public RecordViewHolder(@NonNull View itemView) {
            super(itemView);
            mTvName = itemView.findViewById(R.id.tvRecordItemName);
            mTvTime = itemView.findViewById(R.id.tvRecordItemTime);
            mIvDel = itemView.findViewById(R.id.ivRecordItemDelete);
            mIvPlayPause = itemView.findViewById(R.id.ivRecordItemPlay);
        }

        public void bindData(String audioPath, int position) {
            mBindPath = audioPath;
            mBindPos = position;
            String fileName = FileUtils.getFileName(audioPath);
            mTvName.setText(fileName);
            mTvTime.setText(TimeUtils.millis2String(FileUtils.getFileLastModified(audioPath),new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")));

            mIvDel.setOnClickListener(v -> {
                handleDel(this, position);
            });

            mIvPlayPause.setOnClickListener(v -> {
                handlePlayPause(this, position);
            });
        }
    }

    public interface OnItemClickListener {
        void onItemDelete(int position);
    }

    public void setOnItemClickListener(OnItemClickListener listener) {
        this.listener = listener;
    }
}
